package beusoft.ccjk.com.calculator;

import java.util.Random;

public class myClass {


    private static void println(Object o) {
        System.out.println(o);
    }

    private static void print(Object o) {
        System.out.print(" " + o);
    }

    private static void printArr(int arr[]) {
        println("//////////////////////////");
        print("\n[");
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                print(arr[i]);
            } else {
                print(arr[i] + " , ");
            }

        }
        print("]\n");
        println("//////////////////////////");
    }


    public static void main(String... args) {
        testTime();
//        testSort();

    }

    /**
     * 测试排序是否正却
     */
    private static void testSort() {
//        int[] arr = new int[]{10, 9, 8, 7, 6, 5};
        int[] arr = new int[10];
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(100);
        }
        printArr(arr);
        sort2(arr);
        printArr(arr);

    }

    private static void testTime() {
        int[] arr = new int[10000];
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(100000);
        }
        int[] arr1 = arr.clone();
        int[] arr2 = arr.clone();
        int[] arr3 = arr.clone();


        long start = System.currentTimeMillis();
        sort1(arr1);
        long t = System.currentTimeMillis() - start;
        println("冒泡排序时间：" + t);
        ///////////////////////////

        start = System.currentTimeMillis();
        sort2(arr2);
        t = System.currentTimeMillis() - start;
        println("选择排序时间：" + t);//很多情况下，比冒泡快了4倍以上

        ///////////

        start = System.currentTimeMillis();
        sort3(arr3);
        t = System.currentTimeMillis() - start;
        println("插入排序时间：" + t);//很多情况下，比选择排序快3倍左右
    }

    /**
     * 插入排序
     *
     * @param arr
     */
    public static void sort3(int[] arr) {
        int len = arr.length;
        int out, inner;
        for (out = 1; out < len; out++) {
            int temp = arr[out];
            inner = out;
            //寻找插入点
            while (inner > 0 && arr[inner - 1] > temp) {
                arr[inner] = arr[inner - 1];
                inner--;
            }
            arr[inner] = temp;
        }

    }

    /**
     * 选择排序
     *
     * @param arr
     */
    public static void sort2(int[] arr) {
        int len = arr.length;
        for (int out = 0; out < len - 1; out++) {
            int min = out;
            for (int inner = out + 1; inner < len; inner++) {
                if (arr[inner] < arr[min]) {
                    min = inner;
                }
            }
            if (min != out) {
                swap(arr, min, out);
            }
        }
    }

    /**
     * 冒泡
     *
     * @param arr
     */
    public static void sort1(int[] arr) {
        int length = arr.length;
        for (int out = length - 1; out > 0; out--) {
            for (int inner = 0; inner < out; inner++) {
                if (arr[inner] > arr[inner + 1]) {
                    swap(arr, inner, inner + 1);
                }
            }
        }

    }

    private static void swap(int[] arr, int index1, int index2) {
        int temp = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = temp;
    }


    public void test(int[] arr) {
        int outer, inner, len = arr.length;
        //冒泡
        for (outer = len - 1; outer > 0; outer--) {
            for (inner = 0; inner < outer; inner++) {

            }
        }
        //选择
        int min,temp;
        for (outer = 0; outer < len - 1; outer++) {
            min = outer;
            temp=arr[outer];
            for (inner = outer + 1; inner < len; inner++) {
                if(arr[inner]<arr[min]){
                    min=inner;
                }
            }



        }


    }

}
